package com.yueman.ptoolopenticket.ticket.aop.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yueman.ptoolopenticket.ticket.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.xml.transform.Result;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class CommonSignCheck {

    @Pointcut(value = "@within(com.yueman.ptoolopenticket.ticket.aop.CommSignCheck)")
    public void isResultMethod() {
    }

    @Pointcut(value = "execution(com.yueman.ptoolopenticket.ticket.dto.request.BaseReq *(..))")
    public void signCheck() {
    }

    @Around(value = "isResultMethod()&&signCheck()")
    public Result checkSign(ProceedingJoinPoint joinPoint) throws Throwable {
        //* //获取参数
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        //将参数转化为Json对象
        JSONObject argJson = (JSONObject) JSON.toJSON(arg);
        Map<String, Object> data = JSON.parseObject(argJson.toJSONString(), Map.class);
        log.info("request params : {}", argJson.toString());
        String upChannel = data.get("channel").toString();
        if (upChannel == null) {
            throw new RRException("channel未上送");
        }
        if (upChannel == null) {
            throw new RRException("action未上送");
        }
        log.info("upChannel:{}", upChannel);
        //校验数据
        log.info("当前通道:{}", upChannel);
        Object proceed = null;
        proceed = joinPoint.proceed(args);
        log.info("返回数据{}", JSON.toJSONString(proceed));
        return (Result) proceed;
    }
}
